<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');
if ($db_question && $o_share_qcheck) {
	$qkey = array_rand($db_question);
}
@include_once(D_P.'data/bbscache/o_config.php');
!$o_share_open && Showmsg('share_close');

require_once(R_P.'require/showimg.php');

InitGP(array('see','type'),null,1);
$u = (int)GetGP('u');
!$u && $u = $winduid;
$thisbase = $basename."q=share&";

$where = '';
if ($see == 'friend') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	if ($friends = getFriends($winduid,0,0,false,1)) {
		$uids = array_keys($friends);
		$where .= 's.uid IN('.pwImplode($uids).') AND s.ifhidden=0';
	} else {
		require_once(R_P.'require/header.php');
		require_once PrintEot('m_share');
		footer();
	}
	$username = $windid;
	$thisbase .= "see=$see&";
} elseif ($see == 'favor') {
	list($faceimg) = showfacedesign($winddb['icon'],1,'s');
	$username = $windid;
	$where .= 's.uid='.pwEscape($winduid).' AND s.ifhidden=1';
	$thisbase .= "see=$see&";
} elseif ($see == 'recommend') {
	define('AJAX',1);
	
	if (empty($_POST['step'])) {
		InitGP(array('id'), null, 2);
		$a = 'recommend';
		$rt = $db->get_one("SELECT id,type,content,username FROM pw_share WHERE id=" . pwEscape($id));

		if (empty($rt)) {
			Showmsg('data_error');
		}

		$temp = unserialize($rt['content']);

		$rt['link']	= $temp['link'];
		if ($rt['type']=='user') {
			$title = $temp['user']['username']."($rt[link])";
		} elseif ($rt['type']=='photo') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$title= $belong.$temp['photo']['username']."($rt[link])";
		} elseif ($rt['type']=='album') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$title = $belong.$temp['album']['username']."($rt[link])";
		} elseif ($rt['type']=='group') {
			$title = $temp['group']['name']."($rt[link])";
		} elseif ($rt['type']=='diary') {
			$title = $temp['diary']['subject']."($rt[link])";
		} else {
			$title = $rt['link'];
		}
		$descrip = $temp['descrip'];
		
		$atc_name = getLangInfo('o',$rt['type'],false,1);
		require_once PrintEot('m_ajax');
		ajax_footer();

	} else {

		InitGP(array('sendtoname'));
		InitGP(array('subject','atc_content'),'P');
		
		if(@include(D_P."data/bbscache/wordsfb.php")){
			if ($wordsfb) {
				foreach ($wordsfb as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($subject,$banword)!==false) {
						Showmsg('title_wordsfb');
					} elseif (strpos($atc_content,$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
			if ($replace) {
				foreach ($replace as $key => $value) {
					$banword = (string) stripslashes($key);
					if (strpos($subject,$banword)!==false) {
						Showmsg('title_wordsfb');
					} elseif (strpos($atc_content,$banword)!==false) {
						Showmsg('content_wordsfb');
					}
				}
			}
		}
		
		require_once(R_P.'require/msg.php');

		$userdb = '';
		if ($sendtoname) {
			$userdb = $db->get_one("SELECT username FROM pw_members WHERE username=".pwEscape($sendtoname));
		}

		if (!$userdb) {
			$errorname = $sendtoname;
			Showmsg('user_not_exists');
		}
		if (!$subject || !$atc_content) {
			Showmsg('tofriend_msgerror');
		}
		$msgdb = array(
			'toUser'	=> $userdb['username'],
			'fromUid'	=> $winduid,
			'fromUser'	=> $windid,
			'subject'	=> stripslashes($subject),
			'content'	=> stripslashes($atc_content),
		);
		pwSendMsg($msgdb);
		Showmsg('operate_success');
	}
} else {
	if ($u!=$winduid) {
		$friend = getOneFriend($u);
		if ($friend) {
			$where .= 's.uid='.pwEscape($u).' AND s.ifhidden=0';
			$faceimg = $friend['face'];
			$username = $friend['username'];
		} else {
			Showmsg('mode_o_not_friend');
		}
		$thisbase .= "u=$u&";
	} else {
		list($faceimg) = showfacedesign($winddb['icon'],1,'s');
		$where .= 's.uid='.pwEscape($winduid).' AND s.ifhidden=0';
		$username = $windid;
	}
}
if ($type && in_array($type,array('web','user','photo','album','group','video','music','flash','diary'))) {
	$where .= ($where=='' ? 's.type='.pwEscape($type) : ' AND s.type='.pwEscape($type));
}

$shares = array();
$count = $db->get_value("SELECT count(*) as count FROM pw_share s WHERE $where");
if ($count) {
	$perpage = 10;
	$page = (int)GetGP('page');
	$page<1 && $page = 1;
	$start = ($page-1)*$perpage;
	$numofpage = ceil($count/$perpage);
	
	if (!$db_dopen) {
		$where .= " AND s.type!='diary'";
	}
	if (!$db_phopen) {
		$where .= " AND s.type!='photo'";
	}
	if (!$o_groups_open) {
		$where .= " AND s.type!='group'";
	}


	$rs = $db->query("SELECT s.*,m.groupid FROM pw_share s LEFT JOIN pw_members m ON s.uid=m.uid WHERE $where ORDER BY s.id DESC ".pwLimit($start,$perpage));
	while ($rt = $db->fetch_array($rs)) {
		$rt['postdate'] = get_date($rt['postdate'],'m-d H:s');
		$temp = unserialize($rt['content']);
		if ($temp['video']) {
			$rt['host']	= $temp['video']['host'];
			$rt['hash'] = $temp['video']['hash'];
		}
		$rt['link']	= $temp['link'];
		if ($rt['type']=='user') {
			$temp['link']	= $temp['user']['username'];
			$rt['image']	= $temp['user']['image'];
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".$temp['user']['username']."</a>";
		} elseif ($rt['type']=='photo') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$rt['image']	= $temp['photo']['image'];
			$temp_uid	= $temp['photo']['uid'];
			$rt['title']= $belong."<a href=\"mode.php?m=o&q=user&u=$temp_uid\" target=\"_blank\">".$temp['photo']['username']."</a>";
		} elseif ($rt['type']=='album') {
			$belong	= getLangInfo('o','photo_belong',false,1);
			$rt['image']	= $temp['album']['image'];
			$temp_uid	= $temp['album']['uid'];
			$rt['title']= $belong."<a href=\"mode.php?m=o&q=user&u=$temp_uid\" target=\"_blank\">".$temp['album']['username']."</a>";
		} elseif ($rt['type']=='group') {
			$rt['image']	= $temp['group']['image'];
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".$temp['group']['name']."</a>";
		} elseif ($rt['type']=='diary') {
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".$temp['diary']['subject']."</a>";
		} else {
			$rt['title']= "<a href=\"$rt[link]\" target=\"_blank\">".substrs($rt['link'],40)."</a>";
		}
		$rt['descrip']	= $temp['descrip'];
		if ($rt['ifhidden']) {
			$rt['type_name']	= getLangInfo('o',$rt['type'].'_self',false,1);
		} else {
			$rt['type_name']	= getLangInfo('o',$rt['type'],false,1);
		}
		
		if ($rt['uid']!=$winduid && $friends[$rt['uid']]) {
			$rt['face'] = $friends[$rt['uid']]['face'];
		}
		unset($rt['content']);
		$shares[] = $rt;
	}
	$pages = numofpage($count,$page,$numofpage,$thisbase);
}
require_once(R_P.'require/header.php');
require_once PrintEot('m_share');
footer();
?>